package com.kuang.demo.service.wx;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kuang.demo.bo.zzywx.detail.FinallySpecification;
import com.kuang.demo.bo.zzywx.detail.GoodsType;
import com.kuang.demo.bo.zzywx.detail.Issue;
import com.kuang.demo.bo.zzywx.detail.SpecificationList1;
import com.kuang.demo.bo.zzywx.goods.*;
import com.kuang.demo.mapper.wx.WXFootPrintMapper;
import com.kuang.demo.mapper.wx.WXGoodsMapper;
import com.kuang.demo.mapper.wx.WXUserMapper;
import com.kuang.demo.pojo.wx.Cskaoyanmall_footprint;
import com.kuang.demo.pojo.wx.Cskaoyanmall_goods;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class WXGoodsServiceImpl implements WXGoodsService {
    @Autowired
    WXGoodsMapper wxGoodsMapper;

    @Autowired
    WXFootPrintMapper wxFootPrintMapper;

    @Autowired
    WXUserMapper wxUserMapper;
    @Override
    public ReCategory selectCategory(int id) {
        //首先去判断是不是一级id
        String level = wxGoodsMapper.judgeLevel(id);

        if("L1".equals(level)){
            List<CategoryGoods> brotherCategory = wxGoodsMapper.selectCategory(id);

            ReCategory reCategory = new ReCategory();
            reCategory.setBrotherCategory(brotherCategory);
            reCategory.setCurrentCategory(brotherCategory.get(0));

            CategoryGoods parentCategory = wxGoodsMapper.selectCategoryParent(id);
            reCategory.setParentCategory(parentCategory);

            return reCategory;
        } else{
            ReCategory reCategory = new ReCategory();

            //首先查id对应的一级标题
            int idd = wxGoodsMapper.selectLevelById(id);

            List<CategoryGoods> brotherCategory = wxGoodsMapper.selectCategory(idd);
            reCategory.setBrotherCategory(brotherCategory);
            CategoryGoods parentCategory = wxGoodsMapper.selectCategoryParent(idd);
            reCategory.setParentCategory(parentCategory);
            CategoryGoods currentCategory = wxGoodsMapper.selectByCategory(id);



            return reCategory;
        }

    }

    @Override
    public ReList selectList(ListReceive listReceive) {
        ReList reList = new ReList();


        listReceive.setPage(listReceive.getSize() * (listReceive.getPage() - 1));
        List<GoodsList0> list = wxGoodsMapper.selectGoodsByPid(listReceive);
        int total = wxGoodsMapper.selectNumByPid(listReceive);

        reList.setCount(total);
        reList.setGoodsList(list);

        return reList;
    }

    @Override
    public ReCount selectCountGoods() {
        int num = wxGoodsMapper.countNum();
        ReCount reCount = new ReCount();
        reCount.setGoodsCount(num);
        return reCount;
    }

    @Override
    public FinallySpecification selectGoodsDetail(int id) {
        FinallySpecification finallySpecification = new FinallySpecification();



        //1111111111111111
        //得到了不同的规格
        List<String> valueList = wxGoodsMapper.selectGoodsType(id);

        List<SpecificationList1> list1 = new ArrayList();

        //遍历循环
        for (String s : valueList) {
            SpecificationList1 specificationList1 = new SpecificationList1();
            specificationList1.setName(s);
            List<GoodsType> list = wxGoodsMapper.selectGoodsTypeBySpec(id,s);
            specificationList1.setValueList(list);

            list1.add(specificationList1);
        }

        finallySpecification.setSpecificationList(list1);


        //222222222222222222222
        finallySpecification.setGroupon(new ArrayList<>());



        //33333333333333333
        List<Issue> listIssue = wxGoodsMapper.selectIssue(id);
        finallySpecification.setIssue(listIssue);



        //444444444444444444
        Integer userHasCollect = wxGoodsMapper.selectCollectNum(id);
        finallySpecification.setUserHasCollect(userHasCollect);



        //5555555555555555555
        String image = wxGoodsMapper.selectImage(id);
        finallySpecification.setShareImage(image);



        //6666666666666666666
        List<Commit1> commit1s = wxGoodsMapper.selectComment(id);
        ReComment1 reComment1 = new ReComment1();
        reComment1.setData(commit1s);
        reComment1.setCount(commit1s.size());

        finallySpecification.setComment(reComment1);



        //7777777777777777777777777777
        List<Attribute> attributes = wxGoodsMapper.selectAttribute(id);
        finallySpecification.setAttribute(attributes);



        //88888888888888888
        //先搜品牌id，如果为0则null
        int brandId = wxGoodsMapper.selectBrandId(id);

        Brand1 brand1 = wxGoodsMapper.selectBrandById(brandId);
        finallySpecification.setBrand(brand1);


        //999999999999999999999
        List<Product1> product1s = wxGoodsMapper.selectGoodsProduct(id);
        finallySpecification.setProductList(product1s);


        //1010101010
        GoodsFinal goodsFinal = wxGoodsMapper.selectGoodsAll(id);
        finallySpecification.setInfo(goodsFinal);

        //获得商品id和用户id，新增到footprint表
        //用户id
        Subject subject = SecurityUtils.getSubject();
        String username = (String) subject.getPrincipal();
        int userID = wxUserMapper.selectUserIdByName(username);

        //获得商品id
        int goodsId = id;
        //写入时间

        wxFootPrintMapper.insertGoodsUser(userID,goodsId);

        return finallySpecification;
    }

    @Override
    public List<ReRelated> selectRelated(int id) {
        int categoryId = wxGoodsMapper.selectCategoryId(id);

        List<ReRelated> list = wxGoodsMapper.selectGoodsByCategoryId(categoryId);

        return list;
    }

    @Override
    public List<WxGoodsInfo> selectGoods(Integer size, Integer id, Integer page) {
        Integer offset=(page-1)*size;
        return wxGoodsMapper.selectGoods(offset,size,id);
    }

    @Override
    public List<WxGoodsInfo> selectGoodsByKeyword(Integer page, String keyword, Integer size,String sort,
                                                  String order) {
        Integer offset=(page-1)*size;
        return wxGoodsMapper.selectGoodsByKeyword(offset,size,keyword,sort,order);
    }

    @Override
    public List<WxGoodsInfo> selectHotAndNewGoods(WxCategoryBO categoryBO) {
        List<WxGoodsInfo> goodsInfos=null;
        if (categoryBO.getIsHot() != null) {
            goodsInfos= wxGoodsMapper.selectHotAndNewGoods(categoryBO);
        }else if (categoryBO.getIsNew()!=null){
            goodsInfos= wxGoodsMapper.selectHotAndNewGoods(categoryBO);
        }else if (categoryBO.getBrandId()!=null){
            goodsInfos= wxGoodsMapper.selectHotAndNewGoods(categoryBO);
        }
        return goodsInfos;
    }
}
